package com.controller;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricIdentityLink;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * 组任务
 * @author 123
 *
 */
@RestController
public class GroupTaskController {

	@Autowired
	private ProcessEngine processEngine;
	
	/**
	 * 部署
	 * @return
	 */
	@GetMapping("sequence/deployGroup")
	public String deploySequenceFlow(){
		
		InputStream bpmn = this.getClass().getResourceAsStream("/diagrams/groupTask.bpmn");
		InputStream png = this.getClass().getResourceAsStream("/diagrams/groupTask.png");
		
		Deployment deployment = processEngine.getRepositoryService().createDeployment().name("组任务")
			.addInputStream("groupTask.bpmn", bpmn)//与资源文件名称要一致
			.addInputStream("groupTask.png", png)
			.deploy();
		
		System.err.println(deployment.getKey() + "---" + deployment.getName());
		
		return deployment.getId();
	}
	
	/**
	 * 启动流程实例
	 * @return
	 */
	@RequestMapping("sequence/startGroup")
	public String startSequenceFlow(){
		
		String processDefinitionKey = "groupTask";
		
		//正在执行的流程实例和执行对象相关的service
		RuntimeService runtimeService = processEngine.getRuntimeService();
		
		//使用流程定义的key启动流程实例，key对应的是bpmn文件中的id，默认是最新版本的流程启动
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);
		
		System.out.println(processInstance.getId() + processInstance.getProcessDefinitionId());
		
		return processInstance.getProcessDefinitionId();
	}
	
	/**
	 * 查询当前人的个人任务
	 * @return
	 */
	@RequestMapping("sequence/findGroupOne")
	public String findGateWayTask(){
		
		String assignee = "daF";
		
		List<Task> list = processEngine.getTaskService()
		.createTaskQuery()
		.taskAssignee(assignee).list();
		
		if(!CollectionUtils.isEmpty(list)){
			for (Task task : list) {
				System.out.println(task.getAssignee());
				System.out.println(task.getId());
				System.out.println(task.getName());
				System.out.println(task.getExecutionId());
			}
		}
		
		System.out.println("****************");
		
		return list.toString();
	}
	
	/**
	 * 查询正在执行的的任务办理人表
	 */
	@GetMapping("group/findP")
	public String findPersonal(){
		
		String taskId = "57509";
		
		List<IdentityLink> links = processEngine.getTaskService()
						.getIdentityLinksForTask(taskId);
		
		for (int i = 0; i < links.size(); i++) {
			System.err.println(links.get(i).getUserId());
		}
		
		return String.valueOf(links.size());
	}
	/**
	 * 查询历史任务办理人表
	 */
	@GetMapping("group/findHisP")
	public String findHisPersonal(){
		
		String taskId = "57509";
		
		List<HistoricIdentityLink> links = processEngine.getHistoryService()
						.getHistoricIdentityLinksForTask(taskId);
		
		for (int i = 0; i < links.size(); i++) {
			System.err.println(links.get(i).getUserId());
		}
		
		return String.valueOf(links.size());
	}
	
	/**
	 * 拾取任务，将组任务分给个人任务，指定任务的办理人
	 * @return
	 */
	@GetMapping("group/claim")
	public String claim(){
		
		String taskId = "57509";
		String userId = "daF";
		
		processEngine.getTaskService()
				.claim(taskId, userId);
		
		return "success!";
	}
	
	/**
	 * 将个人任务回退回组任务
	 * @return
	 */
	@GetMapping("group/returnClaim")
	public String returnClaim(){
		
		String taskId = "57509";
		
		processEngine.getTaskService()
				.claim(taskId, null);
		
		return "success!";
	}
	
	/**
	 * 向组任务中添加人
	 * @return
	 */
	@GetMapping("group/addUser")
	public String addGroupUser(){
		
		String taskId = "57509";
		
		processEngine.getTaskService()
					.addCandidateUser(taskId, "大s");
		
		
		return "success!";
	}
	
	/**
	 * 向组任务添删除人
	 * @return
	 */
	@GetMapping("group/delUser")
	public String delGroupUser(){
		
		String taskId = "57509";
		
		String userId = "大s";
		
		processEngine.getTaskService()
			.deleteCandidateUser(taskId, userId);
		
		return "success!";
	}
	
	/**
	 * 完成我的任务
	 * @return
	 */
	@RequestMapping("sequence/completeGroupOne")
	public String completeGateWayTask(){
		
		String taskId = "45004";
		
		//完成任务的同时，设置流程变量，使用流程变量来指定完成任务
		Map<String, Object> varMap = new HashMap<String, Object>();
		varMap.put("money", "3000");
		
		processEngine.getTaskService().complete(taskId,varMap);
		
		return "完成任务" + taskId;
	}
	
	
}
